#include "Framework/Win32/Win32Timer.h"


namespace tyro
{
	namespace Framework
	{


		Win32Timer::Win32Timer()
			:	cpuClock(0)
		{
			// Start the clock
			Reset();
		}

		Win32Timer::~Win32Timer()
		{
		}

		void Win32Timer::Reset()
		{

			/* Get the CPU clock frequency */
			QueryPerformanceFrequency( &cpuFrequency );

			/* Set the first reading */
			QueryPerformanceCounter( &startTime );

			/* Retrieve current CPU clock*/
			cpuClock = clock();
		}

		ULONG_32 Win32Timer::GetMilliseconds()
		{	
			/* Get the next time reading */
			LARGE_INTEGER time;
			QueryPerformanceCounter(&time);

			/* Calculate the elapsed time */
			LARGE_INTEGER elapsedTime;
			elapsedTime.QuadPart = time.QuadPart - startTime.QuadPart;

			/* Convert time to number of miliseconds */
			ULONG_32 elapsedMillis = (ULONG_32) (1000 * elapsedTime.QuadPart / cpuFrequency.QuadPart);

			return elapsedMillis;
		}

		ULONG_32 Win32Timer::GetMicroseconds()
		{
			/* Get the next time reading */
			LARGE_INTEGER time;
			QueryPerformanceCounter(&time);

			/* Calculate the elapsed time */
			LARGE_INTEGER elapsedTime;
			elapsedTime.QuadPart = time.QuadPart - startTime.QuadPart;

			/* Convert time to number of microseconds */
			ULONG32 elapsedMicos = (ULONG_32) (1000000 * elapsedTime.QuadPart / cpuFrequency.QuadPart);

			return elapsedMicos;
		}

		ULONG_32 Win32Timer::GetMillisecondsCPU()
		{
			// Calculate elapsed milliseconds since CPU clock started
			clock_t newClock = clock();
			return (ULONG_32)( (FLOAT_32)( newClock - cpuClock ) / ( (FLOAT_32)CLOCKS_PER_SEC / 1000.0 ) ) ;
		}

		ULONG_32 Win32Timer::GetMicrosecondsCPU()
		{
			// Calculate elapsed microseconds since CPU clock started
			clock_t newClock = clock();
			return (ULONG_32)( (FLOAT_32)( newClock - cpuClock ) / ( (FLOAT_32)CLOCKS_PER_SEC / 1000000.0 ) ) ;
		}

		

	}
}
